import { createSlice } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    counter: 100,
  },
  // reducers中的方法相当于之前的reducer中的一个个case语句
  reducers: {
    addNumber(state, { payload }) {
      state.counter = state.counter + payload
    },
    subNumber(state, { payload }) {
      state.counter = state.counter - payload
    },
  },
})

// 导出action
export const { addNumber, subNumber } = counterSlice.actions

// 导出的reducer
export default counterSlice.reducer
